package com.sofwin.yyb.controller;

import com.sofwin.yyb.entity.Mechanism;
import com.sofwin.yyb.entity.Appointment;
import com.sofwin.yyb.entity.Course;
import com.sofwin.yyb.entity.User;
import com.sofwin.yyb.service.MechanismService;
import com.sofwin.yyb.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/service")
public class CourseController {

    @Autowired
    CourseService courseService;
    @Autowired
    MechanismService mechanismService;

    @RequestMapping("/list")
    @ResponseBody
    public List<Course> serviceList(Integer type){
        return courseService.findAll(type);
    }
    @RequestMapping("/shop")
    public String toShop(Integer id, HttpSession session){
        Course course = courseService.findById(id);
        session.setAttribute("shop",course);
        return "redirect:/app/service.html";
    }
    @RequestMapping("/shopInfo")
    @ResponseBody
    public Course shopInfo(HttpSession session){
        Course services = (Course)session.getAttribute("shop");
        return services;
    }
    @RequestMapping("/serviceForm")
    public String serviceForm(){
        return "redirect:/app/service_form.html";
    }

    /**
     * 商家新增服务
     * @param course
     * @param file
     * @param session
     * @return
     */
    @RequestMapping("/addService")
    @ResponseBody
    public String addService(Course course, @RequestParam(value = "file",required = false)MultipartFile file,HttpSession session){
        if(Objects.isNull(file) || file.isEmpty()){
            return "false";
        }
        String url = file.getOriginalFilename();
        User user = (User)session.getAttribute("user");
        Mechanism mechanism = mechanismService.findByUserId(user.getId());
        course.setCImages("img/"+url);
        course.setMId(mechanism.getMId());
        course.setCStatus(1);
        courseService.addService(course);
        return "true";
    }
    /**
     * 新增订单
     * @param mId
     * @param session
     * @return
     */
    @RequestMapping("/order")
    @ResponseBody
    public String order(Integer cId,HttpSession session){
        User user = (User)session.getAttribute("user");
        Appointment order = new Appointment();
        order.setUserId(user.getId());
        order.setCId(cId);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd/HH:mm");
        //获取当前时间
        String systemTime = sdf.format(date);
        order.setADate(systemTime.substring(0,10));
        order.setATime(systemTime.substring(11,16));
        order.setMStatus(0);
        //添加账单信息
        courseService.addOrder(order);
        return "ok";
    }


}
